/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.cqq.util;

import com.yunhoo.cqq.impl.IbatisDocument;
import com.yunhoo.cqq.model.CodeBase;

/**
 *
 * @author Administrator
 */
public class SysCode {

    public static CodeBase getGradeLevel(String code) {
        CodeBase result = new CodeBase();
        String[] arr = {"3", "5", "7"};
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            if (code.length() == Integer.parseInt(arr[0])) {
                result.setGradeLevel(1);
                break;
            } else {
                if (code.length() == Integer.parseInt(arr[i])) {
                    result.setGradeLevel(i + 1);
                    result.setParentcode(code.substring(0, Integer.parseInt(arr[i - 1])));
                    break;
                }
            }
        }
        return result;
    }

    public static String FormatCode(String flag) {
        String str = "";
        if (flag != null && !"".equals(flag) && flag.contains(",")) {
            String[] kmdms = flag.trim().split(",");
            String str1 = "";
            String str2 = "";
            for (int i = 0; i < kmdms.length; i++) {

                if (i == kmdms.length - 1) {
                    str1 = "'" + kmdms[i] + "'";
                } else {
                    str2 += "'" + kmdms[i] + "',";
                }

            }
            str = str2 + str1;
        } else {
            if (flag != null) {
                str = "'" + flag.trim() + "'";
            } else {
                str = "''";
            }
        }
        return str;
    }

    public static String queryString(String startmonth, String lastmonth) {
        String flag = "";
        if (startmonth != null && !"".equals(startmonth) && lastmonth != null && !"".equals(lastmonth)) {
            Integer startm = Integer.parseInt(startmonth);
            Integer lastm = Integer.parseInt(lastmonth);
            if ("0".equals(startmonth) && "0".equals(lastmonth)) {
                flag = "yj1-yd1+yj2-yd2+yj3-yd3+yj4-yd4+yj5-yd5+yj6-yd6+yj7-yd7+yj8-yd8+yj9-yd9+yj10-yd10+yj11-yd11+yj12-yd12+yj13-yd13";
            }
            if (!"0".equals(startmonth) && !"0".equals(lastmonth)) {
                if (startm <= lastm) {
                    Integer mont = lastm - startm + 1;
                    for (int i = startm; i <= lastm; i++) {
                        if (i == lastm) {
                            flag += "yj" + i + "-" + "yd" + i;
                        } else {
                            flag += "yj" + i + "-" + "yd" + i + "+";
                        }
                    }
                }
            }
        }
        return flag;
    }

    public static CodeBase getGrade(String code, String rulecode) {
        CodeBase result = new CodeBase();
        result.setId(code);
        String splittype = IbatisDocument.getRulecontentById(rulecode);
        String[] arr = splittype.split("-");
        if (arr != null && arr.length > 0) {
            int temp = 0;
            for (int i = 0; i < arr.length; i++) {
                if (code.length() == Double.parseDouble(arr[0])) {
                    result.setGrade(1);
                    break;
                } else {
                    temp += Double.parseDouble(arr[i]);
                    if (code.length() == temp) {
                        result.setGrade(i + 1);
                        result.setParentcode(code.substring(0, temp - Integer.parseInt(arr[i])));
                        break;
                    }
                }

            }
        }
        return result;
    }

    public static void main(String[] args) {
        queryString("0", "0");

    }
}
